package com.zmzncs.lmtc.module.promotion.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.promotion.MonthRentTypeEnum;
import com.zmzncs.lmtc.common.util.ExceptionUtil;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfoDTO;
import com.zmzncs.lmtc.module.promotion.dao.ReserveFeeScaleDao;
import com.zmzncs.lmtc.module.promotion.dao.ReserveRuleParkDao;
import com.zmzncs.lmtc.module.promotion.pojo.duration_rule_fee_scale.DurationRuleFeeScale;
import com.zmzncs.lmtc.module.promotion.pojo.duration_rule_park.DurationRulePark;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule.MonthRule;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule.MonthRuleDTO;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_fee_scale.MonthRuleFeeScale;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_fee_scale.MonthRuleFeeScaleDTO;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_park.MonthRulePark;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_fee_scale.ReserveFeeScale;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_fee_scale.ReserveFeeScaleDTO;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule.ReserveRule;
import com.zmzncs.lmtc.module.promotion.dao.ReserveRuleDao;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule.ReserveRuleAddForm;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule.ReserveRuleDTO;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule.ReserveRuleUpdateForm;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule_park.ReserveRulePark;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule_park.ReserveRuleParkDTO;
import com.zmzncs.lmtc.module.promotion.service.ReserveRuleParkService;
import com.zmzncs.lmtc.module.promotion.service.ReserveRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 促销-预约规则表 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2020-12-14
 */
@Service
public class ReserveRuleServiceImpl extends ServiceImpl<ReserveRuleDao, ReserveRule> implements ReserveRuleService {

    @Autowired
    ReserveRuleDao reserveRuleDao;
    @Autowired
    ReserveRuleParkService reserveRuleParkService;
    @Autowired
    ReserveRuleParkDao reserveRuleParkDao;
    @Autowired
    ReserveFeeScaleDao reserveFeeScaleDao;
    @Autowired
    ParkInfoDao parkInfoDao;

    @Override
    @Transactional
    public void addReserveRule(ReserveRuleAddForm form) {

        //根据规则名称查询预约规则
        List<ReserveRuleDTO> ruleByNameList = reserveRuleDao.getReserveRuleByName(form.getRuleName());
        if (ruleByNameList.size() > 0) {
            ExceptionUtil.businessException("规则名称【" + form.getRuleName() + "】已存在");
        }

        //  已有规则的停车场
        List<ReserveRuleParkDTO> reserveRuleParkList = reserveRuleParkDao.getReserveRuleParkList(null);
        List<Long> hasRuleParkIdList = reserveRuleParkList.stream().map(ReserveRuleParkDTO::getParkId).collect(Collectors.toList());

//        //  校验停车场是否已经有其他预约规则
//        for (Long parkId : form.getParkIds()) {
//
//        }
        if (hasRuleParkIdList.contains(form.getParkId())) {
            ExceptionUtil.businessException("停车场id为" + form.getParkId() + "的停车场已经有其他预约规则");
        }

//        //  如果匹配所有停车场，查询所有停车场id集合
//        if (form.getIfAllParks().equals(1)) {
//            if (reserveRuleParkList.size() != 0) {
//                ExceptionUtil.businessException("已有停车场存在规则，全部停车场不可选");
//            }
//            //  查询所有停车场
//            List<ParkInfoDTO> parkList = parkInfoDao.getParks();
//            List<Long> parkIdList = parkList.stream().map(ParkInfoDTO::getId).collect(Collectors.toList());
//            form.setParkIds(parkIdList);
//        }

        //  添加预约规则
        ReserveRule reserveRule = new ReserveRule();
        BeanUtils.copyProperties(form, reserveRule);
        reserveRule.setParkId(form.getParkId());
        reserveRule.setStatus(0);
        reserveRule.setIsDeleted(0);
        reserveRule.setCreateUserId(2l);
        reserveRule.setCreateTime(new Date());
        reserveRule.setUpdateTime(new Date());
        reserveRuleDao.insert(reserveRule);

        //  添加预约规则的停车场
//        List<ReserveRulePark> addReserveRuleParkList = new ArrayList<>();
//        for (Long parkId : form.getParkIds()) {
//            ReserveRulePark reserveRulePark = new ReserveRulePark();
//            reserveRulePark.setReserveRuleId(reserveRule.getId());   //预约规则id
//            reserveRulePark.setParkId(parkId); //停车场id
//            Integer rate = form.getParkProfitRate();
//            if (rate != null){
//                Double aDouble = rate.doubleValue() / 100;
//                reserveRulePark.setParkProfitRate(new BigDecimal(aDouble)); //车场分成比例
//            }
//            addReserveRuleParkList.add(reserveRulePark);
//        }
//        reserveRuleParkService.saveBatch(addReserveRuleParkList);

        //  添加预约金额0
        if (form.getTimeSlotList() != null && !form.getTimeSlotList().isEmpty()) {
            for (ReserveRuleAddForm.TimeSlot slot : form.getTimeSlotList()) {
                ReserveFeeScale reserveFeeScale = new ReserveFeeScale();
                reserveFeeScale.setAmount(slot.getAmount());//金额
                reserveFeeScale.setReserveBeginTime(slot.getReserveBeginTime());//开始时间
                reserveFeeScale.setReserveEndTime(slot.getReserveEndTime());//结束时间
                reserveFeeScale.setReserveRuleId(reserveRule.getId());  //预约规则ID
                reserveFeeScale.setCreateTime(new Date());
                reserveFeeScale.setUpdateTime(new Date());
                reserveFeeScale.setParkId(form.getParkId());

                reserveFeeScaleDao.insert(reserveFeeScale);
            }
        }
    }

    @Override
    public IPage<ReserveRuleDTO> getReserveRuleParkList(Page page) {
        IPage<ReserveRuleDTO> reserveRuleIPage = reserveRuleDao.getReserveRuleParkList(page);
        //  收费标准
        for (ReserveRuleDTO ruleDTO : reserveRuleIPage.getRecords()) {
            //  查询规则的收费标准
            List<ReserveFeeScaleDTO> reserveFeeScaleDTOList = reserveFeeScaleDao.getReserveRuleFeeScaleByRuleId(ruleDTO.getId());
            ruleDTO.setFeeScaleList(reserveFeeScaleDTOList);
        }

        return reserveRuleIPage;
    }

    @Override
    public void updateReserveRule(ReserveRuleUpdateForm form) {

        //根据规则名称查询预约规则
        List<ReserveRuleDTO> ruleByNameList = reserveRuleDao.getReserveRuleByName(form.getRuleName());
        if (ruleByNameList.size() > 0) {
            ExceptionUtil.businessException("规则名称【" + form.getRuleName() + "】已存在");
        }

        //  已有规则的停车场
        List<ReserveRulePark> reserveRuleParkList = reserveRuleParkDao.selectList(null);
        List<Long> hasRuleParkIdList = reserveRuleParkList.stream().map(ReserveRulePark::getParkId).collect(Collectors.toList());

        //  校验停车场是否已经有其他月租规则
//        for (Long parkId : form.getParkIds()) {
            if (hasRuleParkIdList.contains(form.getParkId())) {
                ExceptionUtil.businessException("停车场id为" + form.getParkId() + "的停车场已经有其他预约规则");
            }
//        }

//        //  如果匹配所有停车场，查询所有停车场id集合
//        if (form.getIfAllParks().equals(1)) {
//            if (reserveRuleParkList.size() != 0) {
//                ExceptionUtil.businessException("已有停车场存在规则，全部停车场不可选");
//            }
//            //  查询所有停车场
//            List<ParkInfoDTO> parkList = parkInfoDao.getParks();
//            List<Long> parkIdList = parkList.stream().map(ParkInfoDTO::getId).collect(Collectors.toList());
//            form.setParkIds(parkIdList);
//        }

        //  更新预约规则
        ReserveRule reserveRule = new ReserveRule();
        BeanUtils.copyProperties(form, reserveRule);
        reserveRule.setUpdateTime(new Date());
        reserveRuleDao.updateById(reserveRule);


        //*********************修改停车场
//        //  删除旧的停车场数据
//        LambdaUpdateWrapper<ReserveRulePark> updateWrapper = new LambdaUpdateWrapper();
//        updateWrapper.eq(ReserveRulePark::getReserveRuleId, form.getId());
//        reserveRuleParkDao.delete(updateWrapper);

//        //  添加月租规则的停车场
//        List<ReserveRulePark> addReserveRuleParkList = new ArrayList<>();
//        for (Long parkId : form.getParkIds()) {
//            ReserveRulePark reserveRulePark = new ReserveRulePark();
//            reserveRulePark.setReserveRuleId(form.getId());   //预约规则id
//            reserveRulePark.setParkId(parkId); //停车场id
//            addReserveRuleParkList.add(reserveRulePark);
//        }
//        reserveRuleParkService.saveBatch(addReserveRuleParkList);

        //  *******************收费标准
        //  删除旧的收费标准
        LambdaUpdateWrapper<ReserveFeeScale> feeScaleLambdaUpdateWrapper = new LambdaUpdateWrapper();
        feeScaleLambdaUpdateWrapper.eq(ReserveFeeScale::getReserveRuleId, form.getId());
        reserveFeeScaleDao.delete(feeScaleLambdaUpdateWrapper);
        //  添加月付金额
        if (form.getTimeSlotList() != null && !form.getTimeSlotList().isEmpty()) {
            for (ReserveRuleUpdateForm.TimeSlot slot : form.getTimeSlotList()) {
                ReserveFeeScale reserveFeeScale = new ReserveFeeScale();
                reserveFeeScale.setReserveBeginTime(slot.getReserveBeginTime());//开始时间
                reserveFeeScale.setReserveEndTime(slot.getReserveEndTime());//结束时间
                reserveFeeScale.setReserveRuleId(form.getId());  //预约规则ID
                reserveFeeScale.setCreateTime(new Date());
                reserveFeeScale.setUpdateTime(new Date());
                reserveFeeScale.setParkId(form.getParkId());

                reserveFeeScaleDao.insert(reserveFeeScale);
            }
        }
    }
}
